<template>
  <div class="page-container">
    <el-card shadow="hover" class="content-card">
      <template #header>
        <h3 class="card-title">所有请假记录</h3>
      </template>
      
      <el-table
        :data="tableData"
        v-loading="loading"
        style="width: 100%"
        stripe
        border
      >
        <el-table-column prop="applicationNo" label="申请编号" width="180" />
        <el-table-column prop="userName" label="申请人" width="120" />
        <el-table-column prop="deptName" label="所属部门" width="150" />
        <el-table-column prop="leaveTypeName" label="请假类型" width="120" />
        <el-table-column prop="startTime" label="开始时间" width="180">
          <template #default="{ row }">
            {{ formatDate(row.startTime) }}
          </template>
        </el-table-column>
        <el-table-column prop="endTime" label="结束时间" width="180">
          <template #default="{ row }">
            {{ formatDate(row.endTime) }}
          </template>
        </el-table-column>
        <el-table-column prop="duration" label="时长(天)" width="100" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.status)">
              {{ getStatusText(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="approverName" label="审批人" width="120" />
        <el-table-column label="操作" width="100" fixed="right">
          <template #default="{ row }">
            <el-button link type="primary" @click="handleView(row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        v-model:current-page="pageNum"
        v-model:page-size="pageSize"
        :total="total"
        :page-sizes="[10, 20, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="loadData"
        @current-change="loadData"
        style="margin-top: 20px; justify-content: flex-end"
      />
    </el-card>

    <!-- 详情对话框 -->
    <el-dialog v-model="dialogVisible" title="请假申请详情" width="600px">
      <el-descriptions :column="2" border v-if="currentRow">
        <el-descriptions-item label="申请编号" :span="2">{{ currentRow.applicationNo }}</el-descriptions-item>
        <el-descriptions-item label="申请人">{{ currentRow.userName }}</el-descriptions-item>
        <el-descriptions-item label="所属部门">{{ currentRow.deptName }}</el-descriptions-item>
        <el-descriptions-item label="请假类型">{{ currentRow.leaveTypeName }}</el-descriptions-item>
        <el-descriptions-item label="请假时长">{{ currentRow.duration }} 天</el-descriptions-item>
        <el-descriptions-item label="开始时间">{{ formatDate(currentRow.startTime) }}</el-descriptions-item>
        <el-descriptions-item label="结束时间">{{ formatDate(currentRow.endTime) }}</el-descriptions-item>
        <el-descriptions-item label="申请时间" :span="2">{{ formatDate(currentRow.createTime) }}</el-descriptions-item>
        <el-descriptions-item label="状态" :span="2">
          <el-tag :type="getStatusType(currentRow.status)">{{ getStatusText(currentRow.status) }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="请假事由" :span="2">{{ currentRow.reason }}</el-descriptions-item>
        <el-descriptions-item v-if="currentRow.approverName" label="审批人">{{ currentRow.approverName }}</el-descriptions-item>
        <el-descriptions-item v-if="currentRow.approvalTime" label="审批时间">{{ formatDate(currentRow.approvalTime) }}</el-descriptions-item>
        <el-descriptions-item v-if="currentRow.approvalOpinion" label="审批意见" :span="2">{{ currentRow.approvalOpinion }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { getAllApplications } from '@/api/leave'

const loading = ref(false)
const tableData = ref([])
const pageNum = ref(1)
const pageSize = ref(10)
const total = ref(0)
const dialogVisible = ref(false)
const currentRow = ref(null)

// 加载数据
const loadData = async () => {
  loading.value = true
  try {
    const res = await getAllApplications(pageNum.value, pageSize.value)
    tableData.value = res.data.records || []
    total.value = res.data.total || 0
  } catch (error) {
    console.error('加载数据失败:', error)
  } finally {
    loading.value = false
  }
}

// 查看详情
const handleView = (row) => {
  currentRow.value = row
  dialogVisible.value = true
}

// 格式化日期
const formatDate = (date) => {
  if (!date) return '-'
  return new Date(date).toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}

// 获取状态文本
const getStatusText = (status) => {
  const statusMap = {
    0: '待审批',
    1: '已通过',
    2: '已拒绝',
    3: '已取消'
  }
  return statusMap[status] || '未知'
}

// 获取状态类型
const getStatusType = (status) => {
  const typeMap = {
    0: 'warning',
    1: 'success',
    2: 'danger',
    3: 'info'
  }
  return typeMap[status] || 'info'
}

onMounted(() => {
  loadData()
})
</script>

<style scoped>
.page-container {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 100%;
}

.content-card {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.content-card :deep(.el-card__body) {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 24px;
  overflow: hidden;
}

.card-title {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
}

.el-table {
  flex: 1;
}
</style>
